<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Detail_Kursus extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
		$this->load->library('session');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('kelas_santri_model');
        $this->load->model('kelas_model');
        $this->load->model('periode_model');
		$this->load->model('santri_model');
    }

    //load Grid
    function index($id_santri) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
			$colModel['Nama_Periode'] = array('Periode', 150, TRUE, 'center', 1);
            $colModel['Nama_Kelas'] = array('Kelas', 250, TRUE, 'center', 1);
            $colModel['Nama_Ustad'] = array('Ustad', 250, TRUE, 'center', 1);
			$colModel['hari'] = array('Hari', 100, TRUE, 'center', 1);
            $colModel['Jam'] = array('Jam', 100, TRUE, 'center', 1);
            $colModel['Nilai'] = array('Nilai Teori', 70, TRUE, 'center', 1);
			$colModel['Nilai_Praktek'] = array('Nilai Praktek', 70, TRUE, 'center', 1);
			
            //get_santri_name
			$hasil = $this->santri_model->selectone($id_santri);
			foreach ($hasil->result() as $row) {
                $nama_santri = $row->nama_santri;
				$munakosah = $row->munakosah;
            }
			$this->session->set_userdata('id_santri', $id_santri);
			$this->session->set_userdata('nama_santri', $nama_santri);

            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Detail Kursus</br>Santri : '.$nama_santri.'</br>No Induk : '.$munakosah.'',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambah Kursus', 'add', 'spt_js');
            // $buttons[] = array('separator');
            // $buttons[] = array('Perbarui Nilai', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Cetak Detail Kursus Santri', 'print', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke Daftar Santri', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_foto
            $grid_js = build_grid_js('flex1', site_url("detail_kursus/grid_foto/".$id_santri.""), $colModel, 'NAMA_PERIODE', 'desc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='Tambah Kursus')
                {
                        location.href='" . base_url() . "index.php/detail_kursus/pilih_kelas';
                }
                
				if (com=='Cetak Detail Kursus Santri')
                {
                        location.href='" . base_url() . "index.php/detail_kursus/to_excel/".$id_santri."';
                }			
				
				if (com=='Kembali ke Daftar Santri')
                {
                        location.href='" . base_url() . "index.php/santri/index/';
                }

                if (com=='Hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' kursus?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/detail_kursus/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_foto($id_santri) {
        $valid_fields = array('NAMA_PERIODE','NAMA_KELAS', 'nama_ustad','nilai', 'nilai_praktek');
        $this->flexigrid->validate_post('ID_KELAS', 'desc', $valid_fields);
        $records = $this->kelas_santri_model->get_data_kelas($id_santri);
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) {
		$kelas = str_replace("\'", "'", $row->NAMA_KELAS);
            $record_items[] = array(
                $row->id_kelas_santri,
                $no = $no + 1,
                $row->NAMA_PERIODE,
                $kelas,
				$row->nama_ustad,
				$row->hari,
                $row->jam,
				$row->nilai,
				$row->nilai_praktek
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

	//export to excel
	function to_excel($id_santri) {
		$query['data_kursus'] = $this->kelas_santri_model->ekspor_data_kelas($id_santri);
		$this->load->view('excel_kursus',$query);
	}
	
	//pilih kelas
    function pilih_kelas() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['Nama_Periode'] = array('Periode', 100, TRUE, 'center', 1);
			$colModel['NAMA_KELAS'] = array('Kelas', 250, TRUE, 'center', 1);
			$colModel['Hari'] = array('Hari', 100, TRUE, 'center', 1);
            $colModel['Jam'] = array('Jam', 100, TRUE, 'center', 1);
            $colModel['Ruang_Kelas'] = array('Ruang Kelas', 250, TRUE, 'center', 1);
            $colModel['nama_ustad'] = array('Ustad', 200, TRUE, 'center', 1);

            $nama_santri = $this->session->userdata('nama_santri');


            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
				'title' => 'Daftar Data Kursus',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambahkan kursus', 'add', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke detail kursus', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_pilih_kelas
            $grid_js = build_grid_js('flex1', site_url("detail_kursus/grid_pilih_kelas"), $colModel, 'Nama_Periode', 'desc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
				if (com=='Kembali ke detail kursus')
                {
                        location.href='" . base_url() . "index.php/detail_kursus/index/".$this->session->userdata('id_santri')."';
                }
				
                if (com=='Tambahkan kursus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Tambahkan ' + $('.trSelected',grid).length + ' kursus?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/detail_kursus/add" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
														alert(data);
														window.location.href = '" . base_url() . "index.php/detail_kursus/index/".$this->session->userdata('id_santri')."';
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_pilih_kelas() {
        $valid_fields = array('NAMA_PERIODE', 'NAMA_KELAS', 'hari','JADWAL_KELAS', 'ruang_kelas', 'nama_ustad');
        $this->flexigrid->validate_post('NAMA_PERIODE', 'desc', $valid_fields);
        $records = $this->kelas_santri_model->get_kelas_per_periode($this->session->userdata('id_periode_aktif'));
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) {
		//$kelas = str_replace("\'", "'", $row->NAMA_KELAS);
			$record_items[] = array(
                $row->ID_KELAS,
                $no = $no + 1,
                $row->NAMA_PERIODE,
                $row->NAMA_KELAS,
                $row->hari,
                $row->jam,
                $row->ruang_kelas,
                $row->nama_ustad
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
		
	
    function add() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
			$id_santri = $this->session->userdata('id_santri');
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
					$data = array(
                        'id_kelas' => $id,
                        'id_santri' => $this->session->userdata('id_santri'),
                        'tanggal_registrasi_kelas' => date('Y-m-d')
                    );
					
					//cek data yang sama
					$sql = $this->db->query("SELECT COUNT(id_kelas_santri) AS CEK FROM kelas_santri WHERE id_kelas = ".$id." AND id_santri = ".$id_santri."");
					$cek = '';
					
					foreach ($sql->result() as $rows) {
						$cek = $rows->CEK;
					}
					
					$msg = '';
					if ($cek > 0) {
						$msg ='Data gagal ditambahkan. Data kursus sudah diambil.';
					}
					else{
						$this->kelas_santri_model->insert($data);
						$msg ="Data kursus berhasil ditambahkan.";
					}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
                }//end if
            }//end foreach
        }
    }
    
    
    
    public function form_nilai(){
		$data['status'] = 'new';
		$data['failed'] = false;
		$data['aa'] = '';
		$data['content'] = $this->load->view('form_nilai', $data, true);
		$this->load->view('main', $data);
	}
	
    function delete() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
  
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
                    $this->kelas_santri_model->delete($id);
                }//end if
            }//end foreach
        }
    }

}
